package camp.anchors;

import campskeleton.CampSkeleton;
import campskeleton.Plan;
import campskeleton.Profile;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import utils.WeakListener.Changed;

/**
 *
 * @author twak
 */
public class JProfileProfileItem extends javax.swing.JPanel
{

    Plan plan;
    Profile profile;
    Map toUpdate;
    Object selected; // tmp object transferred between

    /** Creates new form JProfileProfileItem */
    public JProfileProfileItem()
    {
        initComponents();
    }

    public JProfileProfileItem( Plan plan, Profile profile, Map toUpdate )
    {
        this.plan = plan;
        this.profile = profile;
        this.toUpdate = toUpdate;

        initComponents();

        outCombo.addMouseListener( new MouseAdapter()
        {

            @Override
            public void mouseClicked( MouseEvent e )
            {
                if ( e.getButton() == 3 )
                {
                    Object o = outCombo.getSelectedItem();
                    if ( o instanceof ProfileComboItem )
                        CampSkeleton.instance.setProfile( ((ProfileComboItem) o).p );
                }
            }
        } );

        inLabel.addMouseListener( new MouseAdapter()
        {

            @Override
            public void mouseClicked( MouseEvent e )
            {
                if ( e.getButton() == 3 )
                    CampSkeleton.instance.setProfile( JProfileProfileItem.this.profile );
            }
        } );

        inLabel.setText( profile.toString() );
        setupCombo();
    }

    void setupCombo()
    {
        List<Profile> iles = plan.findProfiles();

        DefaultComboBoxModel cm = new DefaultComboBoxModel();

        cm.addElement( new ComboItem()
        {

            @Override
            public String toString()
            {
                return "X";
            }

            @Override
            public void onSelect()
            {
                toUpdate.put( profile, null );
                CampSkeleton.instance.somethingChanged();
            }
        } );

        for ( final Profile p : plan.findProfiles() )
        {
            Object o;
            cm.addElement( o = new ProfileComboItem( profile, p ) );
            if ( p == toUpdate.get( profile ) )
                cm.setSelectedItem( o );
        }

        cm.addElement( new ComboItem()
        {

            @Override
            public String toString()
            {
                return "new";
            }

            @Override
            public void onSelect()
            {
                toUpdate.put( profile, plan.createNewProfile( null ) );
                CampSkeleton.instance.somethingChanged();
                CampSkeleton.instance.profileListChanged();
                setupCombo();
            }
        } );

        outCombo.setModel( cm );
    }

    abstract class ComboItem
    {

        public abstract String toString();

        public abstract void onSelect();
    }

    class ProfileComboItem extends ComboItem
    {

        Profile p;
        Object profile;

        public ProfileComboItem( Object profile, Profile p )
        {
            this.p = p;
            this.profile = profile;
        }

        @Override
        public String toString()
        {
            return p.toString();
        }

        @Override
        public void onSelect()
        {
            toUpdate.put( profile, p );
            CampSkeleton.instance.somethingChanged();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inLabel = new javax.swing.JLabel();
        outCombo = new javax.swing.JComboBox();

        setLayout(new java.awt.GridLayout(1, 0));

        inLabel.setText("jLabel1");
        add(inLabel);

        outCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        outCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outComboActionPerformed(evt);
            }
        });
        add(outCombo);
    }// </editor-fold>//GEN-END:initComponents

    private void outComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outComboActionPerformed
        ComboItem o = (ComboItem) outCombo.getSelectedItem();
        o.onSelect();
    }//GEN-LAST:event_outComboActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel inLabel;
    private javax.swing.JComboBox outCombo;
    // End of variables declaration//GEN-END:variables
}
